package com.codingfuture.glaciercollect.controller;

import com.codingfuture.glaciercollect.quart.WarmHumidQuartz;
import com.codingfuture.glaciercollect.service.MeterRecordService;
import com.codingfuture.glaciercommon.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Sun
 * @DateTime: 2025/4/15 16:50
 * @Description:
 **/
@RestController
@Api(tags = "采集服务")
public class MeterController {

    @Autowired
    private WarmHumidQuartz warmHumidQuartz;
    @Autowired
    private MeterRecordService recordService;

    /**
     * 修改任务执行时间间隔的接口
     *
     * @param sn              任务组名
     * @param intervalSeconds 时间间隔（秒）
     * @return 操作结果
     */
    @PostMapping("/setInterval")
    @ApiOperation("修改任务执行时间间隔")
    public Result<String> setInterval(@RequestParam String sn, @RequestParam int intervalSeconds) {
        String cronExpression = String.format("0/%d * * * * ?", intervalSeconds);
        warmHumidQuartz.updateInterval(sn, cronExpression);
        Boolean b = recordService.updateInterval(sn, cronExpression);
        if (!b) {
            return Result.err("时间间隔修改失败");
        }
        return Result.success("时间间隔修改成功");
    }

    /**
     * 开启任务
     *
     * @param sn
     * @return
     */
    @GetMapping("/start")
    @ApiOperation("开启任务")
    public Result startDevice(String sn) {
        String s = recordService.selectById(sn);
        if (s == null) {
            return Result.err("该设备正在维修中-----");
        }
        warmHumidQuartz.startDevice(sn, s);
        return Result.success();
    }

}
